<template>
  <div class="table">
    <div class="container">
      <div class="handle-box">
        <el-button type="primary" class="el-icon-plus" @click="add"
          >新增</el-button
        >
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="40"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="username"
          label="用户名"
          width="150"
        ></el-table-column>
        <el-table-column
          prop="truename"
          label="姓名"
          width="150"
        ></el-table-column>
        <el-table-column
          prop="phone"
          label="电话"
          width="150"
        ></el-table-column>
        <el-table-column prop="createtime" label="创建日期"></el-table-column>

        <el-table-column label="功能操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handlepassEdit(scope.$index, scope.row)"
              >修改密码</el-button
            >
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
              >修改</el-button
            >
            <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          @current-change="handleCurrentChange"
          layout="prev, pager, next,jumper"
          :total="allcount"
          :page-size="search.size"
          :current-page.sync="search.page"
        ></el-pagination>
      </div>
    </div>
    <!-- 新增弹出框 -->
    <el-dialog title="新增" :visible.sync="addVisible" width="30%">
      <el-form ref="addobj" label-width="100px" :model="addobj">
        <el-form-item label="用户名">
          <el-input v-model="addobj.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="addobj.password"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="addobj.truename"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="addobj.phone"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="insert">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="修改" :visible.sync="editVisible" width="30%">
      <el-form ref="form" label-width="100px">
        <el-form-item label="姓名">
          <el-input v-model="editobj.truename"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="editobj.phone"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑弹出框 -->
    <el-dialog title="修改密码" :visible.sync="editpassVisible" width="30%">
      <el-form ref="form" label-width="100px">
        <el-form-item label="新密码">
          <el-input v-model="editobj.password1"></el-input>
        </el-form-item>
        <el-form-item label="重复密码">
          <el-input v-model="editobj.password2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="updatepass">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 删除提示框 -->
    <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
      <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteRow">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "basetable",
  data() {
    return {
      url: "./vuetable.json",
      tableData: [],
      multipleSelection: [],
      select_cate: "",
      select_word: "",
      del_list: [],
      is_search: false,
      addVisible: false,
      editVisible: false,
      delVisible: false,
      editpassVisible: false,
      idx: -1,
      search: {
        page: 1,
        size: 10,
        isadmin: "0",
      },
      addobj: {},
      editobj: {},
      allcount: 0,
      count: 0,
      houselist: [],
      loulist: [],
      devicelist: [],
    };
  },
  created() {
    this.getData();
  },
  activated() {
    console.log("ccc");
    this.init();
    this.getData();
  },
  computed: {
    data() {},
  },
  methods: {
    init() {
      this.$axios
        .post(this.$api + "admin/manage/list", this.search)
        .then((res) => {
          console.log("res.data.data", res.data.data);
          this.devicelist = res.data.data.list;
        });
    },
    clear() {
      this.search.louname = "";
      this.search.areaname = "";
      this.search.truename = "";
    },

    // 分页导航
    handleCurrentChange(val) {
      this.search.page = val;
      this.getData();
    },
    // 获取 easy-mock 的模拟数据
    getData() {
      // 开发环境使用 easy-mock 数据，正式环境使用 json 文件
      this.search.sid = this.$route.query.sid;
      this.$axios
        .post(this.$api + "admin/manage/list", this.search)
        .then((res) => {
          this.tableData = res.data.data.list;
          this.allcount = res.data.data.allcount;
          this.count = res.data.data.count;
        });
    },
    search() {
      this.is_search = true;
    },
    formatter(row, column) {
      return row.address;
    },
    filterTag(value, row) {
      return row.tag === value;
    },
    handlepassEdit(index, row) {
      this.editobj = row;
      this.editpassVisible = true;
    },
    handleEdit(index, row) {
      this.idx = index;
      const item = this.tableData[index];
      this.editobj = Object.assign({}, item);
      this.editVisible = true;
    },
    handleDelete(index, row) {
      this.editobj = row;
      this.delVisible = true;
    },
    delAll() {
      const length = this.multipleSelection.length;
      let str = "";
      this.del_list = this.del_list.concat(this.multipleSelection);
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + " ";
      }
      this.$message.error("删除了" + str);
      this.multipleSelection = [];
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    add() {
      this.addVisible = true;
    },
    insert() {
      this.addobj.sid = this.$route.query.sid;
      this.addobj.isadmin = 0;
      this.$axios
        .post(this.$api + "admin/manage/add", this.addobj)
        .then((res) => {
          if (res.data.status == 200) {
            this.$message.success("添加成功");
            this.getData();
          } else {
            this.$message.error("添加失败");
          }
          this.addVisible = false;
          this.addobj.password = "";
          this.addobj.username = "";
          this.addobj.truename = "";
          this.addobj.phone = "";
        });
    },
    // 保存编辑
    update() {
      this.$axios
        .post(this.$api + "admin/manage/edit", this.editobj)
        .then((res) => {
          if (res.data.status == 200) {
            this.$message.success("修改成功");
            this.getData();
          } else {
            this.$message.error("修改失败");
          }
          this.editVisible = false;
        });
    },
    updatepass() {
      this.editobj.password = this.editobj.password1;
      this.$axios
        .post(this.$api + "admin/manage/editpassword", this.editobj)
        .then((res) => {
          if (res.data.status == 200) {
            this.$message.success("修改成功");
            this.getData();
          } else {
            this.$message.error("修改失败");
          }
          this.editVisible = false;
        });
    },
    // 确定删除
    deleteRow(uitem) {
      this.$axios
        .post(this.$api + "admin/manage/del", { id: uitem.id })
        .then((res) => {
          if (res.data.status == 200) {
            this.$message.success("删除成功");
            this.getData();
          } else {
            this.$message.error("删除失败");
          }
          this.delVisible = false;
        });
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.del-dialog-cnt {
  font-size: 16px;
  text-align: center;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
</style>
